package com.hkts.management.unattended.poundroom.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.management.unattended.poundroom.entity.YbMutualCompany;
import com.hkts.management.unattended.poundroom.entity.YbMutualInventory;
import com.hkts.management.unattended.poundroom.entity.YbMutualInventoryTail;
import com.hkts.management.unattended.poundroom.mapper.YbMutualInventoryMapper;
import com.hkts.management.unattended.poundroom.service.IYbMutualInventoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.management.unattended.poundroom.service.IYbMutualInventoryTailService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zy
 * @since 2021-10-13
 */
@Service
@Transactional
public class YbMutualInventoryServiceImpl extends ServiceImpl<YbMutualInventoryMapper, YbMutualInventory> implements IYbMutualInventoryService {


    @Autowired
    private YbMutualInventoryMapper ybMutualInventoryMapper;
    @Autowired
    private IYbMutualInventoryTailService ybMutualInventoryTailService ; ;

    @Override
    public IPage<YbMutualInventory> selectPage(Page<YbMutualInventory> page, YbMutualInventory ybMutualInventory) {
        QueryWrapper<YbMutualInventory> query = Wrappers.query();
        return ybMutualInventoryMapper.selectPage(page, query);
    }


    @Override
    public List<YbMutualInventory> selectList(YbMutualInventory ybMutualInventory) {
        QueryWrapper<YbMutualInventory> query = Wrappers.query();
        return ybMutualInventoryMapper.selectList(query);
    }

    public LocalDateTime getLastUpdateTime(){
        QueryWrapper<YbMutualInventory> query = Wrappers.query();
        query.lambda().orderByDesc(YbMutualInventory::getDoptTime);
        query.last("limit 1");
        List<YbMutualInventory> ybMutualCompanies=ybMutualInventoryMapper.selectList(query);
        return ybMutualCompanies.size()>0?ybMutualCompanies.get(0).getDoptTime():null;
    }
    public void mysqlInsertOrUpdateBath(List<YbMutualInventory> ybMutualCompanies){
        ybMutualInventoryMapper.mysqlInsertOrUpdateBath(ybMutualCompanies);
        List<YbMutualInventoryTail> ybMutualInventoryTails=ybMutualCompanies.stream().map(item->{
            YbMutualInventoryTail inventoryTail=new YbMutualInventoryTail();
            inventoryTail.setcInvCode(item.getcInvCode());
            inventoryTail.setStatus(0);
            inventoryTail.setClosed(1);
            inventoryTail.setOverweightVerify(1);
            inventoryTail.setBalanceVerify(1);
            inventoryTail.setTareVerify(1);
            inventoryTail.setSettlement(0);
            inventoryTail.setWeightDiff(new BigDecimal("0.4"));
            inventoryTail.setDeleted(0);
            String code=item.getcInvCode().substring(0,2);
            if("01".equals(code)||"02".equals(code)||"03".equals(code)){
                inventoryTail.setWarehouseType(2);
            }else {
                inventoryTail.setWarehouseType(0);

            }
            return inventoryTail;
        }).collect(Collectors.toList());
        ybMutualInventoryTailService.insertYbMutualInventoryTailByBatch(ybMutualInventoryTails);

    }
}
